import { expect } from 'chai';
import * as sinon from 'sinon';
import { SetSSHPrivateKey } from '../src/sshKey';
import * as fs from 'fs' // 调整路径以匹配你的文件位置
import * as simpleGit from 'simple-git';
import * as path from 'path';
import proxyquire from 'proxyquire';
import * as step from '@flow-step/step-toolkit'

const workDir = './workspace';

describe('deleteBranch', () => {
    let envStub: sinon.SinonStub;
    let git: any;
    let simpleGitStub: sinon.SinonStub;
    let stepInfoStub: sinon.SinonStub;
    let stepErrorStub: sinon.SinonStub;
    const myRepo = 'my-repo_LBTL';
    const sources = JSON.stringify([
        {
            name: myRepo,
            data: {},
        },
    ]);
    let pathStub = {
        basename: sinon.stub().returns('my-repo_LBTL')
    };


    before(() => {
        if (!fs.existsSync(workDir)) {
            fs.mkdirSync(workDir);
        }
        if (!fs.existsSync(`${workDir}/${myRepo}`)) {
            fs.mkdirSync(`${workDir}/${myRepo}`);
        }

    });

    beforeEach(() => {
        envStub = sinon.stub(process, 'env')
            .value({'PROJECT_DIR': `${workDir}/${myRepo}`, 'SOURCES': sources});
        stepInfoStub = sinon.stub(step, 'info');
        stepErrorStub = sinon.stub(step, 'error');
        git = {
            env: sinon.stub().resolves(),
            cwd: sinon.stub().resolves(),
            branch: sinon.stub().resolves({current: 'current-branch'}),
            push: sinon.stub().resolves()
        }
        simpleGitStub = sinon.stub().returns(git);
    });

    afterEach(() => {
        sinon.restore();
    });

    it('should skip deleting task when on master branch', async () => {
        git.branch.resolves({current: 'master'});
        const deleteBranchProxy = proxyquire('../src/deleteBranch', {
            path: pathStub,
            'simple-git': {simpleGit: simpleGitStub},
        }).deleteBranch;
        await deleteBranchProxy();

        expect(stepInfoStub.calledWithExactly('GIT_CURRENT_BRANCH=master')).to.be.true;
        expect(stepInfoStub.calledWithExactly('Current on master branch, skipping the deleting task')).to.be.true;
        expect((git.push).notCalled).to.be.true;
    });

    it('should delete branch if current branch is not master', async () => {
        git.branch.resolves({current: 'feature-branch'});

        const deleteBranchProxy = proxyquire('../src/deleteBranch', {
            path: pathStub,
            'simple-git': {simpleGit: simpleGitStub},
        }).deleteBranch;

        await deleteBranchProxy();

        expect(stepInfoStub.calledWithExactly('GIT_CURRENT_BRANCH=feature-branch')).to.be.true;
        expect(git.push.calledWithExactly('origin', 'feature-branch', ['--delete'])).to.be.true;
        expect(stepInfoStub.calledWithExactly(`Delete branch feature-branch succeeded`)).to.be.true;
    });

    it('should log and throw an error if deleting branch fails', async () => {
        const error = new Error('Network Issue');
        git.branch.resolves({current: 'feature-branch'});
        git.push.rejects(error);

        const deleteBranchProxy = proxyquire('../src/deleteBranch', {
            path: pathStub,
            'simple-git': {simpleGit: simpleGitStub},
        }).deleteBranch;

        try {
            await deleteBranchProxy();
        } catch (err) {
            expect(err).to.equal(error);
            expect(stepErrorStub.calledWithExactly(`Delete branch feature-branch failed`)).to.be.true;
        }
    });

});

